package com.projectsexception.festivales.setup;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.projectsexception.datandroid.Datandroid;
import com.projectsexception.datandroid.DatandroidException;
import com.projectsexception.datandroid.DatandroidFactory;
import com.projectsexception.festivales.R;
import com.projectsexception.festivales.news.model.Feed;
import com.projectsexception.festivales.util.DateFormater;

public final class ProjectResources {
    
    private static ProjectResources myResources;
    
    private List<MapInfo> maps;
    private List<FeedInfo> feeds;
    private Datandroid data;
    private Context context;
    
    public static void initialize(Context context) {
        if (myResources == null) {
            myResources = new ProjectResources(context);
        }
    }
    
    public static ProjectResources getInstance() {
        if (myResources == null) {
            throw new IllegalStateException("No se ha inicializado el cargador de recursos");
        }
        return myResources;
    }
        
    private ProjectResources(Context context) {
        this.context = context;
        this.data = DatandroidFactory.getDatandroid();
    }
    
    public List<MapInfo> getMaps() {
        if (maps == null) {
            loadMaps();
        }
        return maps;
    }

    private void loadMaps() {
        try {
            data.open(context, R.xml.database);
            maps = data.getAll(MapInfo.class);
        } catch (DatandroidException e) {
            
        }
        data.close();
    }
    
    public List<FeedInfo> getFeeds() {
        if (feeds == null) {
            loadFeeds();
        }
        return feeds;
    }

    private void loadFeeds() {
        try {
            data.open(context, R.xml.database);
            feeds = data.getAll(FeedInfo.class);
        } catch (DatandroidException e) {
            Log.e("ProjectResources", e.getLocalizedMessage());
        }
        data.close();
    }
    
    public void saveFeedCache(Feed feed) {
        try {
            data.open(context, R.xml.database);
            data.deleteAll(Feed.class);
            if (feed != null) {
                data.insert(feed);
            }
            data.close();
        } catch (DatandroidException e) {
            Log.e("ProjectResources", e.getLocalizedMessage());
        }
    }
    
    public Feed loadFeedCache(Integer actualFeed) {
        Feed feed = null;
        try {
            data.open(context, R.xml.database);
            List<Feed> feeds = data.get(Feed.class, "feedInfoId = ?", 
                    new String[] {actualFeed.toString()});
            if (feeds.size() > 0) {
                feed = feeds.get(0);
            }
            data.close();
        } catch (DatandroidException e) {
            Log.e("ProjectResources", e.getLocalizedMessage());
        }
        return feed;
    }
    
    public void deleteAllFeedCache() {
        saveFeedCache(null);
    }
    
    public List<Date> getShowDates() {
        List<Date> showDates = new ArrayList<Date>();
        try {
            data.open(context, R.xml.database);
            SQLiteDatabase db = data.getSQLiteDatabase();
            Cursor c = db.query(true, ShowInfo.class.getSimpleName(), new String[] {"date"}, 
                    null, null, null, null, "date ASC", null);
            if (c != null) {
                while (c.moveToNext()) {
                    showDates.add(DateFormater.parseDate(c.getString(0)));
                }
                c.close();
            }
        } catch (DatandroidException e) {
            
        }
        return showDates;
    }

}
